﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DB_InsertItem (smp_read_character_list, smp_read_item_equip_info)
    /// </summary>
    public class DB_InsertItem : IDBProc
    {
        private Item m_item;

        private enum DBFields : int
        {
            AccountId = 0,
        }

        BaseObject m_CBObject;

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public DB_InsertItem(Item item)
        {
            m_item = item;
        }

        public bool onProcess()
        {
//             DbConnection conn = GameDBManager.Instance.CreateConnection();
//             conn.Open();

            return DB_InsertItem.insertItemToDB(m_item.m_Instance.UID,
                        m_item.m_Instance.nOwnerUID,
                        m_item.m_nAccountID,
                        m_item.m_Instance.nOwnSummonUID,
                        m_item.m_Instance.nAuctionID,
                        m_item.m_Instance.nItemKeepingID,
                        m_item.m_pItemBase.nCode,
                        m_item.m_Instance.nIdx,
                        (int)m_item.m_Instance.Flag,
                        m_item.m_Instance.nCount,
                        m_item.m_Instance.nLevel,
                        m_item.m_Instance.nEnhance,
                        m_item.m_Instance.nDurability,
                        m_item.m_Instance.nCurrentEndurance,
                        (int)m_item.m_Instance.GenerateInfo,
                        (int)m_item.m_Instance.tExpire,
                        m_item.m_Instance.eElementalEffectType,
                        m_item.m_Instance.tElementalEffectExpire,
                        m_item.m_Instance.nElementalEffectAttackPoint,
                        m_item.m_Instance.nElementalEffectMagicPoint);
//            conn.Close();
        }

        public static bool insertItemToDB(long uid, int owner_id, int account_id, int summon_id, int auction_id, int keeping_id, int code, int idx, int flag, long cnt, int level, int enhance, int dura, int endurance, int gcode, int remain_time, Elemental.Type elemental_effect_type, long elemental_effect_expire_time, int elemental_effect_attack_point, int elemental_effect_magic_point)
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "smp_insert_item";
            cmd.CommandType = CommandType.StoredProcedure;

            DateTime dt = Globals.GetDateTimeFromUnixTimestamp((long)elemental_effect_expire_time);

            DbParameter IN_SID = GameDBManager.Instance.CreateInParameter(cmd, "IN_SID", DbType.Int64, uid);
            DbParameter IN_OWNER_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_OWNER_ID", DbType.String, owner_id);
            DbParameter IN_ACCOUNT_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_ACCOUNT_ID", DbType.Int32, account_id);
            DbParameter IN_SUMMON_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_SUMMON_ID", DbType.Int32, summon_id);
            DbParameter IN_AUCTION_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_AUCTION_ID", DbType.Int32, auction_id);
            DbParameter IN_KEEPING_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_KEEPING_ID", DbType.Int32, keeping_id);
            DbParameter IN_CODE = GameDBManager.Instance.CreateInParameter(cmd, "IN_CODE", DbType.Int32, code);
            DbParameter IN_IDX = GameDBManager.Instance.CreateInParameter(cmd, "IN_IDX", DbType.Int32, idx);
            DbParameter IN_FLAG = GameDBManager.Instance.CreateInParameter(cmd, "IN_FLAG", DbType.Int32, flag);
            DbParameter IN_CNT = GameDBManager.Instance.CreateInParameter(cmd, "IN_CNT", DbType.Int64, cnt);
            DbParameter IN_LEVEL = GameDBManager.Instance.CreateInParameter(cmd, "IN_LEVEL", DbType.Int32, level);
            DbParameter IN_ENHANCE = GameDBManager.Instance.CreateInParameter(cmd, "IN_ENHANCE", DbType.Int32, enhance);
            DbParameter IN_ETHEREAL_DURABILITY = GameDBManager.Instance.CreateInParameter(cmd, "IN_ETHEREAL_DURABILITY", DbType.Int32, dura);
            DbParameter IN_ENDURANCE = GameDBManager.Instance.CreateInParameter(cmd, "IN_ENDURANCE", DbType.Int32, endurance);
            DbParameter IN_GCODE = GameDBManager.Instance.CreateInParameter(cmd, "IN_GCODE", DbType.Int32, (int)gcode);
            DbParameter IN_REMAIN_TIME = GameDBManager.Instance.CreateInParameter(cmd, "IN_REMAIN_TIME", DbType.Int32, remain_time);
            DbParameter IN_ELEMENTAL_EFFECT_TYPE = GameDBManager.Instance.CreateInParameter(cmd, "IN_ELEMENTAL_EFFECT_TYPE", DbType.Byte, (byte)elemental_effect_type);
            DbParameter IN_ELEMENTAL_EFFECT_EXPIRE_TIME = GameDBManager.Instance.CreateInParameter(cmd, "IN_ELEMENTAL_EFFECT_EXPIRE_TIME", DbType.DateTime, dt);
            DbParameter IN_ELEMENTAL_EFFECT_ATTACK_POINT = GameDBManager.Instance.CreateInParameter(cmd, "IN_ELEMENTAL_EFFECT_ATTACK_POINT", DbType.Int32, elemental_effect_attack_point);
            DbParameter IN_ELEMENTAL_EFFECT_MAGIC_POINT = GameDBManager.Instance.CreateInParameter(cmd, "IN_ELEMENTAL_EFFECT_MAGIC_POINT", DbType.Int32, elemental_effect_magic_point);

            conn.Open();

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_InsertItem.insertItemToDB Exception: {0}", ex);
            }
            conn.Close();
            return true;

        }

    }

}
